package com.example.myapplication.pages;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.viewpager.widget.ViewPager;

import com.example.myapplication.R;
// 1家人，2朋友，3师生，4同事，5#其他(备注)，-1陌生人(未定义)，可选（最多选择两个）
public class pageActivity extends Activity implements View.OnClickListener, ViewPager.OnPageChangeListener {

    // 菜单标题
    private TextView textFamily, textFriend, textTeacherAndStudent, textColleague, textOther, textStranger;


    private Fragment page1, page2, page3, page4, page5, page6;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page);
        initView(); // 初始化控件
        initEvent();    // 设置按钮监听
        restartBotton();    // 重置链接样式
        // 默认初始化第一个Fragment，改变为选中样式
        textFamily.setTextColor(ContextCompat.getColor(this, R.color.white));
        textFamily.setBackground(ContextCompat.getDrawable(this, R.drawable.button_6));
        initFragment(0);
        // 返回按钮
        findViewById(R.id.relation_category_top).findViewById(R.id.top_return).setOnClickListener(view -> finish());

    }

    private void initEvent() {
        textFamily.setOnClickListener(this);
        textFriend.setOnClickListener(this);
        textTeacherAndStudent.setOnClickListener(this);
        textColleague.setOnClickListener(this);
        textOther.setOnClickListener(this);
        textStranger.setOnClickListener(this);
    }

    private void initView() {
        // 底部菜单4个菜单标题
        textFamily = findViewById(R.id.text_family);
        textFriend = findViewById(R.id.text_friend);
        textTeacherAndStudent = findViewById(R.id.text_teacher_and_student);
        textColleague = findViewById(R.id.text_colleague);
        textOther = findViewById(R.id.text_other);
        textStranger = findViewById(R.id.text_stranger);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.text_family:
                restartBotton();
                textFamily.setTextColor(ContextCompat.getColor(this, R.color.white));
                textFamily.setBackground(ContextCompat.getDrawable(this, R.drawable.button_6));
                initFragment(0);
                break;
            case R.id.text_friend:
                restartBotton();
                textFriend.setTextColor(ContextCompat.getColor(this, R.color.white));
                textFriend.setBackground(ContextCompat.getDrawable(this, R.drawable.button_6));
                initFragment(1);
                break;
            case R.id.text_teacher_and_student:
                restartBotton();
                textTeacherAndStudent.setTextColor(ContextCompat.getColor(this, R.color.white));
                textTeacherAndStudent.setBackground(ContextCompat.getDrawable(this, R.drawable.button_6));
                initFragment(2);
                break;
            case R.id.text_colleague:
                restartBotton();
                textColleague.setTextColor(ContextCompat.getColor(this, R.color.white));
                textColleague.setBackground(ContextCompat.getDrawable(this, R.drawable.button_6));
                initFragment(3);
                break;
            case R.id.text_other:
                restartBotton();
                textOther.setTextColor(ContextCompat.getColor(this, R.color.white));
                textOther.setBackground(ContextCompat.getDrawable(this, R.drawable.button_6));
                initFragment(4);
                break;
            case R.id.text_stranger:
                restartBotton();
                textStranger.setTextColor(ContextCompat.getColor(this, R.color.white));
                textStranger.setBackground(ContextCompat.getDrawable(this, R.drawable.button_6));
                initFragment(5);
                break;

            default:
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        restartBotton();
        switch (position) {
            case 0:
                textFamily.setTextColor(0xff1B940A);
                break;
            case 1:
                textFriend.setTextColor(0xff1B940A);
                break;
            case 2:
                textTeacherAndStudent.setTextColor(0xff1B940A);
                break;
            case 3:
                textColleague.setTextColor(0xff1B940A);
                break;
            case 4:
                textOther.setTextColor(0xff1B940A);
                break;
            case 5:
                textStranger.setTextColor(0xff1B940A);
                break;

            default:
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    // 重置链接样式
    private void restartBotton() {
        // 设置字体颜色（默认黑色）
        textFamily.setTextColor(Color.parseColor("#666666"));
        textFriend.setTextColor(Color.parseColor("#666666"));
        textTeacherAndStudent.setTextColor(Color.parseColor("#666666"));
        textColleague.setTextColor(Color.parseColor("#666666"));
        textOther.setTextColor(Color.parseColor("#666666"));
        textStranger.setTextColor(Color.parseColor("#666666"));
        // 设置背景颜色（默认粉色）
        textFamily.setBackground(ContextCompat.getDrawable(this, R.drawable.button_5));
        textFriend.setBackground(ContextCompat.getDrawable(this, R.drawable.button_5));
        textTeacherAndStudent.setBackground(ContextCompat.getDrawable(this, R.drawable.button_5));
        textColleague.setBackground(ContextCompat.getDrawable(this, R.drawable.button_5));
        textOther.setBackground(ContextCompat.getDrawable(this, R.drawable.button_5));
        textStranger.setBackground(ContextCompat.getDrawable(this, R.drawable.button_5));
    }

    // 隐藏Fragment
    private void hideFragment(android.app.FragmentTransaction transaction) {
        if (page1 != null) {
            transaction.hide(page1);
        }
        if (page2 != null) {
            transaction.hide(page2);
        }
        if (page3 != null) {
            transaction.hide(page3);
        }
        if (page4 != null) {
            transaction.hide(page4);
        }
        if (page4 != null) {
            transaction.hide(page4);
        }
        if (page5 != null) {
            transaction.hide(page5);
        }
        if (page6 != null) {
            transaction.hide(page6);
        }
    }

    // 初始化加载Fragment
    private void initFragment(int index) {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideFragment(transaction);
        switch (index) {
            case 0:
                if (page1 == null) {
                    page1 = new page_1();
                    transaction.add(R.id.relation_fragment_layout, page1);
                } else {
                    transaction.show(page1);
                }
                break;
            case 1:
                if (page2 == null) {
                    page2 = new page_2();
                    transaction.add(R.id.relation_fragment_layout, page2);
                } else {
                    transaction.show(page2);
                }
                break;
            case 2:
                if (page3 == null) {
                    page3 = new page_3();
                    transaction.add(R.id.relation_fragment_layout, page3);
                } else {
                    transaction.show(page3);
                }
                break;
            case 3:
                if (page4 == null) {
                    page4 = new page_4();
                    transaction.add(R.id.relation_fragment_layout, page4);
                } else {
                    transaction.show(page4);
                }
                break;
            case 4:
                if (page5 == null) {
                    page5 = new page_5();
                    transaction.add(R.id.relation_fragment_layout, page5);
                } else {
                    transaction.show(page5);
                }
                break;
            case 5:
                if (page6 == null) {
                    page6 = new page_6();
                    transaction.add(R.id.relation_fragment_layout, page6);
                } else {
                    transaction.show(page6);
                }
                break;
            default:
                break;
        }
        transaction.commit();

    }
}
